/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inventory.actions;

import com.erp.erp.security.logic.SecurityLogic;
import com.erp.erp.security.pojo.Companies;
import com.erp.erp.security.pojo.CompanyModuleForms;
import com.erp.erp.security.pojo.CompanyModuleReports;
import com.erp.erp.security.pojo.CompanyModules;
import com.erp.erp.security.pojo.Forms;
import com.erp.erp.security.pojo.Modules;
import com.erp.erp.security.pojo.Reports;
import com.erp.erp.security.pojo.Stores;
import com.erp.erp.security.pojo.Users;
import com.inventory.helperbeans.CompanyFormHelperBean;
import com.inventory.helperbeans.CompanyFormReportHelperBean;
import com.inventory.helperbeans.CompanyReportHelperBean;
import com.inventory.helperbeans.CompanyRoleHelperBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author maha
 */
public class CompanyAction extends ActionSupport implements ServletRequestAware {

    private Vector<Companies> companies = new Vector<Companies>();
    private Vector<String> companyField = new Vector<String>();
    private Users usersByCreatedBy;
    private int parentCompanyId;
    private String companyName;
    private String companyfield;
    private File fileUpload;
    private String fileUploadContentType;
    private String fileUploadFileName;
    private String contactPersonName;
    private String contactPersonEmail;
    private String companyPhone;
    private String companyFax;
    private String companyAddress;
    private String comments;
    private boolean activeFlag;
    private HttpServletRequest servletRequest;
    private int storeCode;
    private String storeName;
    private Vector<Modules> modules = new Vector<Modules>();
    private int moduleId;
    private String moduletitle;
    private String moduleName;
    private int moduleVersion;
    private Vector<CompanyModules> companyModules = new Vector<CompanyModules>();
    private CompanyFormReportHelperBean formReportBean;
    private CompanyRoleHelperBean companyRole;

    public CompanyAction() {
    }

    public String loadCompanies() throws Exception {
        SecurityLogic security = new SecurityLogic();
        setCompanies(security.getCompanies());

        //==========Load Company Fields============
        companyField.add("Oil Industry");
        companyField.add("Information Technology");
        companyField.add("Various Services");
        setCompanyField(companyField);
        return "done";
    }

    public String addCompany() {

        Companies parent = new Companies();
        parent.setCompanySid(parentCompanyId);

        Map<String, Object> session = ActionContext.getContext().getSession();
        Users user = (Users) session.get("user");

        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(dt);

        String filePath = servletRequest.getSession().getServletContext().getRealPath("/uploads/companylogo/" + companyName + "/");
        System.out.println("Server path:" + filePath);
        File fileToCreate = new File(filePath, this.fileUploadFileName);
        try {
            FileUtils.copyFile(this.fileUpload, fileToCreate);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        //============preparing object============
        Companies company = new Companies();
        company.setActiveFlag(activeFlag);
        company.setCompanyName(companyName);
        company.setCompanyField(companyfield);
        company.setContactPersonName(contactPersonName);
        company.setContactPersonEmail(contactPersonEmail);
        company.setCompanyPhone(companyPhone);
        company.setCompanyFax(companyFax);
        company.setComments(comments);
        company.setCompanyAddress(companyAddress);
        if (parentCompanyId != -1) {
            company.setCompanies(parent);
        }
        company.setUsersByCreatedBy(user);
        company.setCreationDate(new Date());
        company.setCompanyLogo(filePath);

        //===============================================

        SecurityLogic logic = new SecurityLogic();
        int addCompany = logic.addCompany(company);
        System.out.println(addCompany);





        return "done";
    }

    public String addStore() {
        Companies parent = new Companies();
        parent.setCompanySid(parentCompanyId);

        Map<String, Object> session = ActionContext.getContext().getSession();
        Users user = (Users) session.get("user");

        try {
            Stores store = new Stores();
            store.setActiveFlag(activeFlag);
            store.setAddress(companyAddress);
            store.setComment(comments);
            store.setCompanies(parent);
            store.setContactPersonName(contactPersonName);
            store.setContactPersonEmail(contactPersonEmail);
            //=========need add contact person email======
            store.setCreationDate(new Date());
            if (companyFax != null) {
                store.setFax(companyFax);
            }
            store.setPhone(companyPhone);
            store.setStoreCode(storeCode);
            store.setStoreName(storeName);
            store.setUsersByCreatedBy(user);
            SecurityLogic security = new SecurityLogic();
            int addStore = security.addStore(store);
            System.out.println(addStore);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "done";
    }

    public String goToCompanyModules() {
        SecurityLogic security = new SecurityLogic();
        setModules(security.getModules());
        return "done";
    }

    public String addCompanyModule() {

        Map<String, Object> session = ActionContext.getContext().getSession();
        Users user = (Users) session.get("user");
        Companies company = (Companies) session.get("company");

        CompanyModules companyModules = new CompanyModules();
        companyModules.setUsersByCreatedBy(user);
        companyModules.setCompanies(company);
        companyModules.setModuleTitle(moduletitle);
        companyModules.setActiveFlag(activeFlag);
        companyModules.setCreationDate(new Date());
        Modules module = new Modules();
        module.setModuleSid(moduleId);
        companyModules.setModules(module);
        SecurityLogic security = new SecurityLogic();
        int addCompanyModule = security.addCompanyModule(companyModules);
        System.out.println(addCompanyModule);
        return "done";
    }

    public String toModule() {
        System.out.println("toModule");
        return "done";
    }

    public String addModule() {
        System.out.println("modules");
        Map<String, Object> session = ActionContext.getContext().getSession();
        Users user = (Users) session.get("user");

        Modules module = new Modules();
        module.setActiveFlag(activeFlag);
        module.setCreationDate(new Date());
        module.setModuleVersion(moduleVersion);
        module.setModuleName(moduleName);
        module.setUsersByCreatedBy(user);
        SecurityLogic logic = new SecurityLogic();
        int addModule = logic.addModule(module);
        System.out.println(addModule);
        return "done";
    }

    public String toCompanyFormsAndReports() {
        Map<String, Object> session = ActionContext.getContext().getSession();
        Companies company = (Companies) session.get("company");
        SecurityLogic security = new SecurityLogic();
        Vector<CompanyModules> companyModules1 = security.getCompanyModules(company.getCompanySid());
        setCompanyModules(companyModules1);
        return "done";
    }

    public String addCompanyFormAndReport() {
        Map<String, Object> session = ActionContext.getContext().getSession();
        Users user = (Users) session.get("user");

        SecurityLogic security = new SecurityLogic();

//        if (getFormReportBean().getCompanyForm() != null) {
//            Collection<CompanyFormHelperBean> companyForm = getFormReportBean().getCompanyForm();
//            Iterator<CompanyFormHelperBean> iterator = companyForm.iterator();
//            while (iterator.hasNext()) {
//                CompanyFormHelperBean companyFormHelperBean = iterator.next();
//                System.out.println(companyFormHelperBean);
//
//                CompanyModuleForms cmf = new CompanyModuleForms();
//
//                cmf.setUsersByCreatedBy(user);  //--------------------------1-----------
//                cmf.setCreationDate(new Date()); //-------------------------2-----------
//
//                if (companyFormHelperBean.getActiveFlag() == null) {
//                    cmf.setActiveFlag(false); //------------3----------------------
//                } else {
//                    cmf.setActiveFlag(companyFormHelperBean.getActiveFlag());
//                }
//
//                CompanyModules cmod = new CompanyModules();
//                cmod.setCompanyModuleSid(companyFormHelperBean.getModuleId());
//                cmf.setCompanyModules(cmod); //-----------------------4-------------
//
//                Forms f = new Forms();
//                f.setFormSid(companyFormHelperBean.getFormId());
//                cmf.setForms(f); //-------------5-----------------------
//
//                cmf.setFormTitle(companyFormHelperBean.getFormTitle()); //-------------6--------
//
//
//
//                int addCompanyModuleForm = security.addCompanyModuleForm(cmf);
//
//
//            }
//        }


        if (getFormReportBean().getCompanyForm() != null) {
            Collection<CompanyFormHelperBean> companyForm = getFormReportBean().getCompanyForm();
            Iterator<CompanyFormHelperBean> iterator1 = companyForm.iterator();
            while (iterator1.hasNext()) {
                CompanyFormHelperBean companyFormHelperBean = iterator1.next();
                System.out.println(companyFormHelperBean);
                CompanyModuleForms cmr = new CompanyModuleForms();
                cmr.setUsersByCreatedBy(user);
                cmr.setCreationDate(new Date());
                if (companyFormHelperBean.getActiveFlag() == null) {
                    cmr.setActiveFlag(false);
                } else {
                    cmr.setActiveFlag(companyFormHelperBean.getActiveFlag());
                }
                cmr.setFormTitle(companyFormHelperBean.getFormTitle());
                CompanyModules cmod = new CompanyModules();
                cmod.setCompanyModuleSid(companyFormHelperBean.getModuleId());
                cmr.setCompanyModules(cmod);
                Forms r = new Forms();
                r.setFormSid(companyFormHelperBean.getFormId());
                cmr.setForms(r);
                int addCompanyModuleForm = security.addCompanyModuleForm(cmr);
                System.out.println(addCompanyModuleForm);
            }
        }



        if (getFormReportBean().getCompanyReport() != null) {
            Collection<CompanyReportHelperBean> companyReport = getFormReportBean().getCompanyReport();
            Iterator<CompanyReportHelperBean> iterator1 = companyReport.iterator();
            while (iterator1.hasNext()) {
                CompanyReportHelperBean companyReportHelperBean = iterator1.next();
                System.out.println(companyReportHelperBean);
                CompanyModuleReports cmr = new CompanyModuleReports();
                cmr.setUsersByCreatedBy(user);
                cmr.setCreationDate(new Date());
                if (companyReportHelperBean.getActiveFlag() == null) {
                    cmr.setActiveFlag(false);
                } else {
                    cmr.setActiveFlag(companyReportHelperBean.getActiveFlag());
                }
                cmr.setReportTitle(companyReportHelperBean.getReportTitle());
                CompanyModules cmod = new CompanyModules();
                cmod.setCompanyModuleSid(companyReportHelperBean.getModuleId());
                cmr.setCompanyModules(cmod);
                Reports r = new Reports();
                r.setReportSid(companyReportHelperBean.getReportId());
                cmr.setReports(r);
                int addCompanyModuleReport = security.addCompanyModuleReport(cmr);
                System.out.println(addCompanyModuleReport);
            }
        }


        return "done";
    }

    public String toCompanyRoles() {
        Map<String, Object> session = ActionContext.getContext().getSession();
        Companies company = (Companies) session.get("company");
        SecurityLogic security = new SecurityLogic();
        Vector<CompanyModules> companyModules1 = security.getCompanyModules(company.getCompanySid());
        setCompanyModules(companyModules1);
        return "done";
    }

    //____________________Accessors(Setters/Getters)__________
    public Vector<Companies> getCompanies() {
        return companies;
    }

    public void setCompanies(Vector<Companies> companies) {
        this.companies = companies;
    }

    public Vector<String> getCompanyField() {
        return companyField;
    }

    public void setCompanyField(Vector<String> companyField) {
        this.companyField = companyField;
    }

    public Users getUsersByCreatedBy() {
        return usersByCreatedBy;
    }

    public void setUsersByCreatedBy(Users usersByCreatedBy) {
        this.usersByCreatedBy = usersByCreatedBy;
    }

    public int getParentCompanyId() {
        return parentCompanyId;
    }

    public void setParentCompanyId(int parentCompanyId) {
        this.parentCompanyId = parentCompanyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getCompanyfield() {
        return companyfield;
    }

    public void setCompanyfield(String companyfield) {
        this.companyfield = companyfield;
    }

    public String getContactPersonName() {
        return contactPersonName;
    }

    public void setContactPersonName(String contactPersonName) {
        this.contactPersonName = contactPersonName;
    }

    public String getContactPersonEmail() {
        return contactPersonEmail;
    }

    public void setContactPersonEmail(String contactPersonEmail) {
        this.contactPersonEmail = contactPersonEmail;
    }

    public String getCompanyPhone() {
        return companyPhone;
    }

    public void setCompanyPhone(String companyPhone) {
        this.companyPhone = companyPhone;
    }

    public String getCompanyFax() {
        return companyFax;
    }

    public void setCompanyFax(String companyFax) {
        this.companyFax = companyFax;
    }

    public String getCompanyAddress() {
        return companyAddress;
    }

    public void setCompanyAddress(String companyAddress) {
        this.companyAddress = companyAddress;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public boolean isActiveFlag() {
        return activeFlag;
    }

    public void setActiveFlag(boolean activeFlag) {
        this.activeFlag = activeFlag;
    }

    public File getFileUpload() {
        return fileUpload;
    }

    public void setFileUpload(File fileUpload) {
        this.fileUpload = fileUpload;
    }

    public String getFileUploadContentType() {
        return fileUploadContentType;
    }

    public void setFileUploadContentType(String fileUploadContentType) {
        this.fileUploadContentType = fileUploadContentType;
    }

    public String getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(String fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

    public int getStoreCode() {
        return storeCode;
    }

    public void setStoreCode(int storeCode) {
        this.storeCode = storeCode;
    }

    public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }

    public Vector<Modules> getModules() {
        return modules;
    }

    public void setModules(Vector<Modules> modules) {
        this.modules = modules;
    }

    public int getModuleId() {
        return moduleId;
    }

    public void setModuleId(int moduleId) {
        this.moduleId = moduleId;
    }

    public String getModuletitle() {
        return moduletitle;
    }

    public void setModuletitle(String moduletitle) {
        this.moduletitle = moduletitle;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public int getModuleVersion() {
        return moduleVersion;
    }

    public void setModuleVersion(int moduleVersion) {
        this.moduleVersion = moduleVersion;
    }

    public Vector<CompanyModules> getCompanyModules() {
        return companyModules;
    }

    public void setCompanyModules(Vector<CompanyModules> companyModules) {
        this.companyModules = companyModules;
    }

    public CompanyFormReportHelperBean getFormReportBean() {
        return formReportBean;
    }

    public void setFormReportBean(CompanyFormReportHelperBean formReportBean) {
        this.formReportBean = formReportBean;
    }

    public CompanyRoleHelperBean getCompanyRole() {
        return companyRole;
    }

    public void setCompanyRole(CompanyRoleHelperBean companyRole) {
        this.companyRole = companyRole;
    }
}
